<template>
  <el-card>
    <div class="function">
      <el-input
        v-model="addVal"
        placeholder="请输入内容"
        class="addInp"
        clearable
      />
      <el-button type="primary" class="add-button" @click="add_button()"
        >添加模板</el-button
      >
    </div>
    <div class="datalist">
      <el-table :data="tableData" style="width: 100%">
        <el-table-column label="id">
          <template slot-scope="scope">
            <span style="margin-left: 10px">{{ scope.row.id }}</span>
          </template>
        </el-table-column>
        <el-table-column label="名称">
          <template slot-scope="scope">
            <span v-if="scope.row.amend == 0" style="margin-left: 10px">{{
              scope.row.name
            }}</span>
            <el-input
              v-else-if="scope.row.amend == 2"
              v-model="scope.row.name"
              placeholder="请输入内容"
              clearable
            />
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button
              v-if="scope.row.amend == 0"
              size="mini"
              @click="handleEdit(scope.$index, scope.row)"
              >编辑</el-button
            >
            <el-button
              v-if="scope.row.amend == 0"
              size="mini"
              @click="zhang(scope.$index, scope.row.id)"
              >详情</el-button
            >
            <el-button
              v-if="scope.row.amend == 0"
              size="mini"
              @click="handelCopy(scope.row)"
              >复制</el-button
            >
            <el-button
              v-if="scope.row.amend == 0"
              size="mini"
              @click="delTemplate(scope.row.id)"
              >删除</el-button
            >
            <div v-else-if="scope.row.amend == 2">
              <el-button size="mini" @click="amendinput(scope.row)"
                >确认修改
              </el-button>
              <el-button
                size="mini"
                @click="cancelEdit(scope.$index, scope.row)"
                >取消修改
              </el-button>
            </div>
          </template>
        </el-table-column>
      </el-table>
    </div>
  </el-card>
</template>

<script>
import {
  gettemplate,
  puttemplate,
  posttemplate,
  getstemplate,
  deltemplate,
  templateCopy
} from "../../api/template";
export default {
  data() {
    return {
      tableData: [],
      addVal: "",
      odlName: "",
    };
  },
  created() {
    this.datalist();
  },
  methods: {
    // 获取数据
    async datalist() {
      const d = { params: { pagenum: this.pagenum, pagesize: this.pagesize } };
      const data = await gettemplate(d);
      this.total = data.count;
      data.data.forEach((item, index) => {
        item["amend"] = 0;
        item["tjadd"] = true;
      });
      this.tableData = data.data;
    },
    //模糊搜索
    async mh() {
      const d = {
        keyword: this.input,
        pagenum: this.pagenum,
        pagesize: this.pagesize,
      };
      const data = await getstemplate(d);
      this.total = data.count;
      data.data.forEach((item, index) => {
        item["amend"] = 0;
        item["tjadd"] = true;
      });
      this.tableData = data.data;
      // this.datalist()
      // console.log(this.input);
    },
    // 修改输入框弹出
    handleEdit(index, row) {
      this.odlName = row.name;
      row.amend = 2;
    },
    // 取消修改
    cancelEdit(index, row) {
      row.amend = 0;
      this.tableData[index].name = this.odlName;
    },
    // 修改数据
    async amendinput(row) {
      const d = { id: row.id, name: row.name };
      const data = await puttemplate(d);
      console.log(data);
      if (data.code == "200") {
        row.amend = 0;
        this.$message({
          message: "修改模板名称成功",
          type: "success",
        });
      }
      // this.datalist();
    },
    // 添加数据
    async add_button() {
      if (!this.addVal.length) {
        this.$message.error("模板名称不能为空");
        return;
      }
      const d = { name: this.addVal };
      const data = await posttemplate(d);
      if (data.code == "200") {
        this.addVal = "";
        this.$message({
          message: "添加模板成功",
          type: "success",
        });
      }
      this.datalist();
    },
    async zhang(index, row) {
      // console.log(row);
      this.$router.push({
        path: "/template/particulars/particulars",
        query: { id: row },
      });
    },
    handleSizeChange(val) {
      this.pagesize = val;
      this.datalist();
    },
    handleCurrentChange(val) {
      this.pagenum = val;
      this.datalist();
    },
    // 删除模板
    async delTemplate(id) {
      this.$confirm("此操作将永久删除模板该, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(async () => {
          const res = await deltemplate(id);
          if (res.code == 200) {
            this.$message({
              type: "success",
              message: "删除成功!",
            });
            this.datalist();
          }
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    
    async handelCopy(dd){
      let res = await templateCopy(dd)
      console.log(res)
      if(res.code == 200){
          this.$message({
              type: "success",
              message: "复制成功!",
            });
         this.datalist()
      }
    }

  },
};
</script>
<style scoped>
.function {
  width: 100%;
  margin: 10px 0;
}

.datalist {
  width: 100%;
}

.addInp {
  width: 20%;
  margin: 0 10px;
}

.dibutyl {
  width: 100%;
}
</style>